'use strict';

var helper = function() {
  return {
    getElementSelector: function(elem, unique) {
      if (!elem || !elem.localName) return;
      var label = elem.localName;
      if (elem.id) {
        label += '#' + elem.id;
      }
      if (elem.classList) {
        for (var i = 0; i < elem.classList.length; i++) {
          label += '.' + elem.classList[i];
        }
      }
      console.log(unique, 'u');
      if (unique) {

        // console.log('unique');
        // var siblings = $(elem).siblings();
        // console.log(siblings, 'si');
        // if (parent != null) {
        //   // console.log(siblings);
        //   if (siblings.length > 1) {
        //     console.log('children');
        //     var index = siblings.index($(elem));
        //     console.log(index, 'index');
        //     label += ':nth-child(' + index + ')'
        //   }
        // }
      }
      return label;
    },
    getElementPath: function(elem, flag) {
      var paths = [];
      var that = this;
      // console.log(flag, 'un');
      // paths.push(that.getElementSelector(elem, flag));
      // $(elem).parents().not('body').not('html').each(function() {
      //   var entry = that.getElementSelector(this, false);
      //   paths.push(entry);
      // });
      paths.push(this.getElementSelector(elem, flag));
      for (elem = elem.parentNode; elem && elem.nodeType == Node.ELEMENT_NODE; elem =
        elem.parentNode) {
        if (elem.localName == 'body') break;
        var selector = this.getElementSelector(elem, false);
        paths.push(selector);
      }
      return paths.reverse().join(' > ');
    },
    getElemPath: function(elem) {
      var array = [];
      var obj = {};
      // console.log(this.isElement(elem), 'is');
      obj.id = elem.id.trim();
      obj.tag = elem.tagName.toLowerCase();
      obj.classname = elem.className.trim();

      while (obj.tag.indexOf('body') == -1) {

        var tempClass = '';
        var tempId = '';
        if (obj.classname.length) {
          console.log(elem.classList, 'list');
          console.log(obj.classname, 'list');
          tempClass = '.' + obj.classname.replace(/\s{2,}/, ' ').split(
            ' ').join('.');
        }
        if (obj.id.length) {
          tempId = '#' + obj.id;
        }
        var str = obj.tag + tempId + tempClass;
        array.push(str);
        elem = $(elem).parent()[0];

        obj.id = elem.id.trim();
        obj.tag = elem.tagName.toLowerCase();
        obj.classname = elem.className.trim();
      }
      return array.reverse().join(' > ');
    },
    getXPath: function(elt) {
      return getElementXPath(elt);

      function getElementXPath(elt) {
        var path = "";
        for (; elt && elt.nodeType == 1; elt = elt.parentNode) {
          var idx = getElementIdx(elt);
          var xname = elt.tagName.toLowerCase();
          if (idx > 1) xname += "[" + idx + "]";
          path = "/" + xname + path;
        }
        return path;
      }

      // Get Idx of an element
      function getElementIdx(elt) {
        var count = 1;
        for (var sib = elt.previousSibling; sib; sib = sib.previousSibling) {
          if (sib.nodeType == 1 && sib.tagName == elt.tagName) count++
        }
        return count;
      }

    }
  };
};

helper.$inject = [];
var app = angular.module('webpageApp');
app.factory('helper', helper);
